package com.jy.fxf_phone.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.jy.fxf_phone.R;
import com.jy.fxf_phone.service.BGMService;

public class SettingActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    CheckBox cbSound;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        toolbar.setNavigationIcon(R.drawable.feed_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        cbSound = (CheckBox) findViewById(R.id.cbSound);
        cbSound.setOnCheckedChangeListener(this);

        sharedPreferences = getSharedPreferences("Sound", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        int isOpen = sharedPreferences.getInt("sound", -1);
        if (isOpen == 0 || isOpen == -1) {
            cbSound.setChecked(true);
        } else if (isOpen == 1) {
            cbSound.setChecked(false);
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            cbSound.setText("开");
            startService(new Intent(this, BGMService.class));
            editor.putInt("sound", 0);
        } else {
            cbSound.setText("关");
            stopService(new Intent(this, BGMService.class));
            editor.putInt("sound", 1);
        }
        editor.apply();
    }


}
